//题目控制。
Ext.define("ES.controller.ItemsController",{
    extend:"Ext.app.Controller",
    models:["ItemsModel","ItemTypesModel","CatalogModel","TeachingSectionsModel"],
    stores:["ItemsStore","ItemTypesToBindStore","CatalogStore","SectionsToBindStore"],
    views:["items.ItemsList","items.ItemsEdit"],
    init:function(){
        this.control({
            "itemsTabList":{
//                afterrender:function(cmp){
//                     var btn = cmp.down("toolbar button[action=btnQuery]");
//                     this.onSearch(btn);
//                },
                itemdblclick:this.onEdit
            },
            "itemsTabList button[action=btnQuery]":{
                click:this.onSearch
            },
            "itemsTabList button[action=btnAdd]":{
                click:this.onAdd
            },
            "itemsTabList button[action=btnDelete]":{
                click:this.onDelete
            },
            "itemsEdit combo[name=catalogID]":{
                beforerender:function(combo){
                    var store = combo.getStore();
                    if(store){
                        store.load();
                    }
                },
                select:function(combo,records,eOpts ){
                    var fs = combo.up("fieldset");
                    if(fs && records[0]){
                        var cb = fs.down("combo[name=sectionID]")
                        if(cb){
                            var st = cb.getStore();
                            if(st){
                                cb.setValue("");
                                st.load({
                                    params:{catalogID:records[0].data.catalogID}
                                })
                            }
                        }
                    }
                }
            },
            "itemsEdit combo[name=typeID]":{
                select:function(combo,records,eOpts ){
                    var frm = combo.up("form");
                    if(frm && records[0]){
                        try{
                            var ec = records[0].data.editClass;
                            if(ec){
                               var cmp = frm.getComponent(ec);
                               if(!cmp){
                                   var old = frm.getComponent(1);
                                   if(old){
                                       frm.remove(old,true);
                                   }
                                   frm.add(Ext.create(ec,{
                                        columnWidth:1
                                   }));
                               }
                            }
                        }catch(e){
                            Ext.Msg.alert("发生异常,该题型未有编辑类存在", e);
                        }
                    }
                }
            },
            "itemsEdit button[action=btnSave]":{
                click:function(btn){
                    var frm = btn.up("form");
                    if(frm){
                        var obj = frm.getComponent(1);
                        if(obj){
                            var err;
                             if(obj.verifyData(err)){
                                 this.onSave(btn,obj)
                             }else{
                                 Ext.Msg.alert("警告",err);
                             }
                        }else{
                            this.onSave(btn)
                        }
                    }
                }
            },
            "itemsEdit button[action=btnCancel]":{
                click:function(btn){
                     var win = btn.up("itemsEdit");
                     if(win){
                         win.close();
                     }
                }
            }
        });
    },
    onSearch:function(btn){
        var toolbar =  btn.ownerCt;
        var grid = toolbar.ownerCt;       
        var st = grid.getStore();
        st.loadPage(1,{
            params:{
                catalogName:toolbar.getComponent(1).getValue(),
                sectionName:toolbar.getComponent(3).getValue(),
                content:toolbar.getComponent(5).getValue()
            }
        });
    },
    onAdd:function(){
         var win = Ext.widget("itemsEdit");
         if(win){
             win.show();
         }
    },
    onEdit:function(view, record, item,index,e,opt){
        var win =Ext.widget("itemsEdit");
        if(win){   
            win.show();
            var el = win.getEl(); 
            el.mask("正在加载数据，请稍后....");
            if(record){
                var frm = win.down("form");
                if(frm){
                    var obj = frm.getComponent(1);
//                    st.load({
//                        params:{itemID:record.data.itemID},
//                        callback:function(records, operation, success){
//                            if(success){
//                                
//                            }
//                        }
//                    });
                    frm.loadRecord(record);
                }
            }
            el.unmask();
        }
    },
    onSave:function(btn,obj){
        var win = btn.up("itemsEdit");
        var form = win.down("form").getForm();
        if(form.isValid()){
            var store = this.getItemsStoreStore();
            var record = form.getRecord();
            var values = form.getValues();
            if(record){
                record.set(values);
            }else{
                record = this.getItemsModelModel().create();
                record.set(values);
                store.insert(0,record);
            }
            if(obj){
                record.set("items", obj.SaveItemsData());
                record.set("answers", obj.SaveAnswerData());
            }
            store.sync();
            win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                        Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                        var st = grid.getStore();
                        Ext.Array.each(data, function(record){
                            st.remove(record);
                        })
                        st.sync();
                }
            }
        });
    }
})